import urllib2
import xml.dom.minidom
import os
import datetime
import Tkinter
import TkProgressBar
#DOM Implementation

APIKey = 'apikey=212cb7425933bf78a2dbdbacf143582890dd1263'

class feed:
    tags = {}

    def __init__(self,name=None,articles=[]):
        self.sourceName = name
        self.articles = articles

    def getArticlesByFilter(self, articleFilterList):
        filteredList = []
        for x in self.articles:
            if x in articleFilterList:
                filteredList += [x]
        return filteredList

    def copy(self):
        return feed(name=self.sourceName,articles=self.articles)
    
class URLfeed(feed):

    def __init__(self, feedXML_URL):
        self.url = feedXML_URL
        self.articles = []
        
        feedXML = urllib2.urlopen(self.url)
        domTree = xml.dom.minidom.parse(feedXML)
        root = domTree.documentElement
        self.sourceName = root.getElementsByTagName('title')[0]\
                              .firstChild\
                              .data\
                              .encode('UTF-8','ignore')
        for x in root.getElementsByTagName('item'):
            self.articles += [article(x,self)]

class DUMMYfeed(feed):

    def __init__(self,root):
        self.url = 'DUMMYPLUG'
        self.articles = []
        
        self.sourceName = root.getElementsByTagName('title')[0]\
                              .firstChild\
                              .data\
                              .encode('UTF-8','ignore')
        for x in root.getElementsByTagName('item'):
            self.articles += [DUMMYarticle(x,self)]

class article():
    textAlchemy = 'http://access.alchemyapi.com/calls/url/URLGetText?'
    tagsAlchemy = 'http://access.alchemyapi.com/calls/url/URLGetRankedNamedEntities?'

    def __init__(self, root, feedSource):
        self.source = feedSource
        self.headline = root.getElementsByTagName('title')[0]\
                            .firstChild\
                            .data\
                            .encode('UTF-8','ignore')
        self.url = root.getElementsByTagName('link')[0]\
                       .firstChild\
                       .data\
                       .encode('UTF-8','ignore')
        self.tags = {}
        self.tags = self.getTags()
        
    def getTags(self):
        if len(self.tags) > 0:
            return self.tags
        else:
            resultXML = xml.dom.minidom.parse(urllib2.urlopen(article.tagsAlchemy + APIKey + '&url=' + self.url))
            root = resultXML.documentElement
            newTags = {}
            for x in root.getElementsByTagName('entity'):
                relevance = x.getElementsByTagName('relevance')[0]\
                             .firstChild\
                             .data
                if float(relevance) > 0.1:
                    tagName = x.getElementsByTagName('text')[0]\
                               .firstChild\
                               .data\
                               .encode('UTF-8','ignore')
                    if not(feed.tags.has_key(tagName)):
                        feed.tags[tagName] = tag(x,self)
                    else:
                        feed.tags[tagName].articles[self] = float(relevance)
                    newTags[tagName] = feed.tags[tagName]
            return newTags

    def getText(self):
#	return "Testing text for when you don't got yourself some interwebs in your tubes."
        resultXML = xml.dom.minidom.parse(urllib2.urlopen(article.textAlchemy + APIKey + '&url=' + self.url))
	root = resultXML.documentElement
	if root.getElementsByTagName('status')[0].firstChild.data.encode('iso-8859-15','ignore') == 'ERROR':
		return 'THIS IS DEFAULT TEXT. THE LIMIT FOR YOUR APIKEY HAS BEEN REACHED!'
	else:
		return root.getElementsByTagName('text')[0]\
			.firstChild\
			.data\
			.encode('UTF-8','ignore')
    def compareTags(self, secondArticle):
        sum = 0.0
        for x in self.tags.values():
            if x in secondArticle.tags.values():
                sum += x.articles[self] * x.articles[secondArticle]
        return sum

class DUMMYarticle(article):

    def __init__(self, root, feedSource):
        self.source = feedSource
        self.headline = root.getElementsByTagName('title')[0]\
                            .firstChild\
                            .data\
                            .encode('UTF-8','ignore')
        self.url = root.getElementsByTagName('link')[0]\
                       .firstChild\
                       .data\
                       .encode('UTF-8','ignore')
        self.tags = {}
        self.tags = self.getTags(root)

    def getTags(self,root):
        if len(self.tags) > 0:
            return self.tags
        else:
            newTags = {}
            for x in root.getElementsByTagName('tag'):
                tagName = x.getElementsByTagName('text')[0]\
                            .firstChild\
                            .data\
                            .encode('UTF-8','ignore')
                if not(feed.tags.has_key(tagName)):
                    feed.tags[tagName] = tag(x,self)
                else:
                    relevance = x.getElementsByTagName('relevance')[0]\
                                 .firstChild\
                                 .data
                    feed.tags[tagName].articles[self] = float(relevance)
                newTags[tagName] = feed.tags[tagName]
            return newTags

class tag():

    def __init__(self,root,parentArticle):
        self.text = root.getElementsByTagName('text')[0]\
                        .firstChild\
                        .data\
                        .encode('UTF-8','ignore')
        self.category = root.getElementsByTagName('type')[0]\
                            .firstChild\
                            .data\
                            .encode('UTF-8','ignore')
        relevance = root.getElementsByTagName('relevance')[0]\
                             .firstChild\
                             .data
        self.articles = {parentArticle:float(relevance)}
        


#####

        

def create(feeds):
    plug = open(os.getcwd()+'\\DUMMYPLUG_System\\DUMMYPLUG.xml','w')
    DOMTree = xml.dom.minidom.Document()
    root = DOMTree.createElement('DUMMYPLUG')
    root.setAttribute('date',datetime.datetime.now().strftime('M%mD%dY%Y'))
    DOMTree.appendChild(root)

    for x in feeds:
        feed = DOMTree.createElement('channel')
        root.appendChild(feed)
    
        title = DOMTree.createElement('title')
        feed.appendChild(title)
        title.appendChild(DOMTree.createTextNode(x.sourceName))
    
        for y in x.articles:
            article = DOMTree.createElement('item')
            feed.appendChild(article)

            headline = DOMTree.createElement('title')
            article.appendChild(headline)
            headline.appendChild(DOMTree.createTextNode(y.headline))

            url = DOMTree.createElement('link')
            article.appendChild(url)
            url.appendChild(DOMTree.createTextNode(y.url))

            tags = DOMTree.createElement('tags')
            article.appendChild(tags)
            for z in y.tags.values():
                tag = DOMTree.createElement('tag')
                tags.appendChild(tag)

                text = DOMTree.createElement('text')
                tag.appendChild(text)
                text.appendChild(DOMTree.createTextNode(z.text))

                relevance = DOMTree.createElement('relevance')
                tag.appendChild(relevance)
                relevance.appendChild(DOMTree.createTextNode(str(z.articles[y])))

                category = DOMTree.createElement('type')
                tag.appendChild(category)
                category.appendChild(DOMTree.createTextNode(z.category))

    plugXML = DOMTree.toprettyxml(indent='')
    i = 0
    while i < (len(plugXML)-3):
        if (plugXML[i:i+2] == '>\n') and not(plugXML[i+2] == '<'):
            plugXML = plugXML[:i+1] + plugXML[i+2:]
        else:
            if (plugXML[i:i+2] == '\n<') and not(plugXML[i-1] == '>'):
                plugXML = plugXML[:i] + plugXML[i+1:]
        i += 1
    plug.write(plugXML)
    plug.close()

def load():
    DOMTree = xml.dom.minidom.parse(open(os.getcwd()+'\\DUMMYPLUG_System\\DUMMYPLUG.xml','r'))
    root = DOMTree.documentElement
    feeds = []
    for x in root.getElementsByTagName('channel'):
        print 'LOADING: ' + x.getElementsByTagName('title')[0].firstChild.data + '\n...'
	feeds += [DUMMYfeed(x)]
	print 'FINISHED\n\n'
    return feeds

